package com.ruoyi.qxk.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.qxk.util.SMSUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.qxk.domain.Rescue;
import com.ruoyi.qxk.service.IRescueService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 应急救援任务Controller
 * 
 * @author ruoyi
 * @date 2025-05-12
 */
@RestController
@RequestMapping("/rescue")
public class RescueController extends BaseController
{
    @Autowired
    private IRescueService rescueService;

    //RabbitTemplate
    @Autowired
    private RabbitTemplate rabmp;
    /**
     * 查询应急救援任务列表
     */
    @RequiresPermissions("qxk:rescue:list")
    @GetMapping("/list")
    public TableDataInfo list(Rescue rescue)
    {
        startPage();
        List<Rescue> list = rescueService.selectRescueList(rescue);
        return getDataTable(list);
    }
    /**
     * 接单
     */
    @PutMapping("/st/{id}/{status}/{phone}")
    public AjaxResult updateStatus(
            @PathVariable Long id,
            @PathVariable Integer status,
            @PathVariable String phone
    ) {
        System.out.println("发送给用户短信：" + phone);
//        SMSUtil.send(phone,"99999");   //通知用户接单成功
        return toAjax(rescueService.updateStatus(id, status));
    }

    /**
     * 导出应急救援任务列表
     */
    @RequiresPermissions("qxk:rescue:export")
    @Log(title = "应急救援任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Rescue rescue)
    {
        List<Rescue> list = rescueService.selectRescueList(rescue);
        ExcelUtil<Rescue> util = new ExcelUtil<Rescue>(Rescue.class);
        util.exportExcel(response, list, "应急救援任务数据");
    }

    /**
     * 获取应急救援任务详细信息
     */
    @RequiresPermissions("qxk:rescue:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(rescueService.selectRescueById(id));
    }

    /**
     * 新增应急救援任务
     */
//    @RequiresPermissions("qxk:rescue:add")
    @Log(title = "应急救援任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Rescue rescue)
    {
        int i = rescueService.insertRescue(rescue);
        if (i>0){
            //发送信息给交换机
            rabmp.convertAndSend("Help_exchange1","Help.add",rescue);
            return AjaxResult.success("添加成功");
        }else {
            return AjaxResult.error("添加失败");
        }
//        return toAjax(rescueService.insertRescue(rescue));
    }

    /**
     * 修改应急救援任务
     */
    @RequiresPermissions("qxk:rescue:edit")
    @Log(title = "应急救援任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Rescue rescue)
    {
        return toAjax(rescueService.updateRescue(rescue));
    }

    /**
     * 删除应急救援任务
     */
    @RequiresPermissions("qxk:rescue:remove")
    @Log(title = "应急救援任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(rescueService.deleteRescueByIds(ids));
    }
}
